package by.bsu.scs.util.runner;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import by.bsu.scs.db.dao.CourseHome;
import by.bsu.scs.db.dao.DisciplineHome;
import by.bsu.scs.db.dao.DisciplineInstructorHome;
import by.bsu.scs.db.dao.DisciplineStudentGroupHome;
import by.bsu.scs.db.dao.DisciplineTaskHome;
import by.bsu.scs.db.dao.GroupHome;
import by.bsu.scs.db.dao.MarkPolicyHome;
import by.bsu.scs.db.dao.MessageHome;
import by.bsu.scs.db.dao.SpecializationHome;
import by.bsu.scs.db.dao.StudentGroupHome;
import by.bsu.scs.db.dao.TaskHome;
import by.bsu.scs.db.dao.UserHome;
import by.bsu.scs.db.dao.UserRoleHome;
import by.bsu.scs.model.Course;
import by.bsu.scs.model.Discipline;
import by.bsu.scs.model.DisciplineInstructor;
import by.bsu.scs.model.DisciplineStudentGroup;
import by.bsu.scs.model.DisciplineTask;
import by.bsu.scs.model.Group;
import by.bsu.scs.model.MarkPolicy;
import by.bsu.scs.model.Message;
import by.bsu.scs.model.Specialization;
import by.bsu.scs.model.StudentGroup;
import by.bsu.scs.model.Task;
import by.bsu.scs.model.User;
import by.bsu.scs.model.UserRole;
import by.bsu.scs.security.MD5Encoder;

/**
 * 
 * DBFiller fills database with some test data.
 * 
 * @author Katherina_Prohorenko
 * 
 */
public class DBFiller {

	public static void main(String[] args) {
		// fillUserRole();
		// fillUser();
		// fillSpecialization();
		// fillCourse();
		// fillGroup();
		// fillStudentGroup();
		// fillDiscipline();
		// fillMarkPolicy();
		// fillTask();
		// fillDisciplineInstructor();
		// fillDisciplineTask();
		//fillDisciplineStudentGroup();// don't work
		// fillMessage("blinov@gmail.com","sova@bsu.by");
		 updateUserPasswords();
	}

	private static void fillMessage(String fromEmail, String toEmail) {
		UserHome userHome = new UserHome();
		User from = new User();
		User exampleFrom = new User(fromEmail);
		from = (User) userHome.findByExample(exampleFrom).get(0);

		User to = new User();
		User exampleTo = new User(toEmail);
		to = (User) userHome.findByExample(exampleTo).get(0);

		Message msg = new Message(new Date(Calendar.getInstance()
				.getTimeInMillis()), false, from, to, "Hello!", null);
		MessageHome messageHome = new MessageHome();
		messageHome.persist(msg);

		// Message[] message = {
		// new Message(new Date(Calendar.getInstance().getTimeInMillis()),
		// false, (User) (new UserHome()).findByExample(
		// new User("varvara@gmail.com")).get(0),
		// (User) new UserHome().findByExample(
		// new User("blinov@gmail.com")).get(0), "Java",
		// "varvara", null, true),
		// new Message(new Date(Calendar.getInstance().getTimeInMillis()),
		// false, (User) new UserHome().findByExample(
		// new User("helen@gmail.com")).get(0),
		// (User) new UserHome().findByExample(
		// new User("blinov@gmail.com")).get(0), "Java",
		// "helen", null, true),
		// new Message(new Date(Calendar.getInstance().getTimeInMillis()),
		// false, (User) new UserHome().findByExample(
		// new User("aleksandr@gmail.com")).get(0),
		// (User) new UserHome().findByExample(
		// new User("blinov@gmail.com")).get(0), "Java",
		// "alexsandr", null, true) };
		// MessageHome dao = new MessageHome();
		// for (Message m : message) {
		// dao.persist(m);
		// }
	}

	private static void fillDisciplineStudentGroup() {
		DisciplineStudentGroup[] dsg = {
				new DisciplineStudentGroup(
						(new StudentGroupHome()).findById(16),
						(new DisciplineTaskHome()).findById(1)),
				new DisciplineStudentGroup(
						(new StudentGroupHome()).findById(17),
						(new DisciplineTaskHome()).findById(1)),
				new DisciplineStudentGroup(
						(new StudentGroupHome()).findById(18),
						(new DisciplineTaskHome()).findById(1)),
				new DisciplineStudentGroup(
						(new StudentGroupHome()).findById(19),
						(new DisciplineTaskHome()).findById(1)),
				new DisciplineStudentGroup(
						(new StudentGroupHome()).findById(20),
						(new DisciplineTaskHome()).findById(1)),
				new DisciplineStudentGroup(
						(new StudentGroupHome()).findById(21),
						(new DisciplineTaskHome()).findById(1)) };
		DisciplineStudentGroupHome dao = new DisciplineStudentGroupHome();

		for (DisciplineStudentGroup d : dsg) {
			dao.persist(d);
		}
	}

	private static void fillDisciplineTask() {
		User instructor = new User();
		Discipline disc = new Discipline();
		disc = (Discipline) new DisciplineHome().findByExample(
				new Discipline("UML")).get(0);
		instructor = (User) new UserHome().findByExample(
				new User("blinov@gmail.com")).get(0);
		DisciplineInstructor di = new DisciplineInstructor(instructor, disc);

		DisciplineInstructorHome diHome = new DisciplineInstructorHome();
		diHome.persist(di);

		TaskHome taskHome = new TaskHome();
		Task task = taskHome.findById(3);

		DisciplineTask discTask = new DisciplineTask(7, di, task);

		DisciplineTaskHome dao = new DisciplineTaskHome();
		dao.persist(discTask);

	}

	private static void fillDisciplineInstructor() {
		DisciplineInstructor[] discInstr = {
				new DisciplineInstructor((User) new UserHome().findByExample(
						new User("blinov@gmail.com")).get(0),
						(Discipline) new DisciplineHome().findByExample(
								new Discipline("Java")).get(0)),
				new DisciplineInstructor((User) new UserHome().findByExample(
						new User("blinov@gmail.com")).get(0),
						(Discipline) new DisciplineHome().findByExample(
								new Discipline("Uml")).get(0)) };
		DisciplineInstructorHome dao = new DisciplineInstructorHome();
		for (DisciplineInstructor dI : discInstr) {
			dao.persist(dI);
		}
	}

	private static void fillTask() {
		Task task = new Task("задание", "название задания", new Date(Calendar
				.getInstance().getTimeInMillis()), 3,
				(MarkPolicy) (new MarkPolicyHome()).findByExample(
						new MarkPolicy("политика1")).get(0));
		TaskHome dao = new TaskHome();
		dao.persist(task);
	}

	private static void fillMarkPolicy() {
		MarkPolicy mPolicy = new MarkPolicy("политика1", "политика1");
		MarkPolicyHome dao = new MarkPolicyHome();
		dao.persist(mPolicy);
	}

	private static void fillDiscipline() {
		User user = new User();
		user.setEmail("blinov@gmail.com");
		user.setPassword("password7");
		Discipline[] disc = { new Discipline("Java"), new Discipline("Uml") };
		DisciplineHome dao = new DisciplineHome();
		for (Discipline d : disc) {
			dao.persist(d);
		}
	}

	private static void fillUserRole() {
		UserRole[] roles = { new UserRole(1, "admin"),
				new UserRole(2, "student"), new UserRole(3, "instructor") };
		UserRoleHome dao = new UserRoleHome();
		for (UserRole ur : roles) {
			dao.persist(ur);
		}
	}

	private static void fillSpecialization() {
		Specialization[] spec = { new Specialization("веб-программирование"),
				new Specialization("экономика") };
		SpecializationHome dao = new SpecializationHome();
		for (Specialization sp : spec) {
			dao.persist(sp);
		}

	}

	private static void fillCourse() {
		Course[] cour = { new Course(1, 1), new Course(2, 2), new Course(3, 3),
				new Course(4, 4), new Course(5, 5), new Course(6, 6) };
		CourseHome dao = new CourseHome();
		for (Course c : cour) {
			dao.persist(c);
		}

	}

	private static void fillGroup() {
		Group[] group = {
				new Group(1, new Course(1, 1), 2,
						(Specialization) (new SpecializationHome())
								.findByExample(
										new Specialization(
												"веб-программирование")).get(0)),
				new Group(2, new Course(1, 1), 6,
						(Specialization) (new SpecializationHome())
								.findByExample(new Specialization("экономика"))
								.get(0)),
				new Group(3, new Course(1, 1), 9,
						(Specialization) (new SpecializationHome())
								.findByExample(
										new Specialization(
												"веб-программирование")).get(0)),
				new Group(4, new Course(2, 2), 2,
						(Specialization) (new SpecializationHome())
								.findByExample(
										new Specialization(
												"веб-программирование")).get(0)),
				new Group(5, new Course(2, 2), 6,
						(Specialization) (new SpecializationHome())
								.findByExample(new Specialization("экономика"))
								.get(0)),
				new Group(6, new Course(2, 2), 9,
						(Specialization) (new SpecializationHome())
								.findByExample(
										new Specialization(
												"веб-программирование")).get(0)),
				new Group(7, new Course(3, 3), 2,
						(Specialization) (new SpecializationHome())
								.findByExample(
										new Specialization(
												"веб-программирование")).get(0)),
				new Group(8, new Course(3, 3), 6,
						(Specialization) (new SpecializationHome())
								.findByExample(new Specialization("экономика"))
								.get(0)),
				new Group(9, new Course(3, 3), 9,
						(Specialization) (new SpecializationHome())
								.findByExample(
										new Specialization(
												"веб-программирование")).get(0)) };
		GroupHome dao = new GroupHome();
		for (Group gr : group) {
			dao.persist(gr);
		}

	}

	private static void fillStudentGroup() {
		StudentGroup[] stGroup = {
				new StudentGroup((new GroupHome()).findById(1),
						(User) (new UserHome()).findByExample(
								new User("maria@gmail.com")).get(0)),
				new StudentGroup((new GroupHome()).findById(1),
						(User) (new UserHome()).findByExample(
								new User("kate@gmail.com")).get(0)),
				new StudentGroup((new GroupHome()).findById(1),
						(User) (new UserHome()).findByExample(
								new User("marmal@gmail.com")).get(0)),
				new StudentGroup((new GroupHome()).findById(3),
						(User) (new UserHome()).findByExample(
								new User("helen@gmail.com")).get(0)),
				new StudentGroup((new GroupHome()).findById(3),
						(User) (new UserHome()).findByExample(
								new User("varvara@gmail.com")).get(0)),
				new StudentGroup((new GroupHome()).findById(3),
						(User) (new UserHome()).findByExample(
								new User("aleksandr@gmail.com")).get(0)) };
		StudentGroupHome dao = new StudentGroupHome();
		for (StudentGroup sgr : stGroup) {
			dao.persist(sgr);
		}
	}

	private static void fillUser() {
		UserRole adminRole = new UserRole();
		adminRole.setRoleName("admin");

		UserRole studentRole = new UserRole();
		studentRole.setRoleName("student");

		UserRole instructorRole = new UserRole();
		instructorRole.setRoleName("instructor");

		UserRoleHome userRoleDao = new UserRoleHome();

		// User[] users = {
		// new User(650503, "Екатерина", "Прохоренко",
		// "katherina.prohorenko@inbox.com",
		// (UserRole) userRoleDao.findByExample(adminRole).get(0),
		// "password1"),
		// new User(800000, "Алексей", "Хурсевич", "hursevich@gmail.com",
		// (UserRole) userRoleDao.findByExample(adminRole).get(0),
		// "password2"),
		// new User(800001, "Вячеслав", "Белокурский",
		// "slavchoo2@gmail.com", (UserRole) userRoleDao
		// .findByExample(adminRole).get(0), "password2"),
		// new User(800002, "Светлана", "Войтех", "yasvedko@gmail.com",
		// (UserRole) userRoleDao.findByExample(adminRole).get(0),
		// "password4"),
		// new User(800003, "Ольга", "Гавлас", "olga.gavlas@gmail.com",
		// (UserRole) userRoleDao.findByExample(adminRole).get(0),
		// "password5"),
		// new User(800004, "Иван", "Иванов", "ivan@gmail.com",
		// (UserRole) userRoleDao.findByExample(studentRole)
		// .get(0), "password6"),
		// new User(700000, "Игорь", "Блинов", "blinov@gmail.com",
		// (UserRole) userRoleDao.findByExample(instructorRole)
		// .get(0), "password7"),
		// new User(800005, "Елена", "Гращенко", "helen@gmail.com",
		// (UserRole) userRoleDao.findByExample(studentRole)
		// .get(0), "password8"),
		// new User(800006, "Варвара", "Тимофеева", "varvara@gmail.com",
		// (UserRole) userRoleDao.findByExample(studentRole)
		// .get(0), "password9"),
		// new User(800007, "Александр", "Маслюк", "aleksandr@gmail.com",
		// (UserRole) userRoleDao.findByExample(studentRole)
		// .get(0), "password10"),
		// new User(800008, "Мария", "Пршай", "maria@gmail.com",
		// (UserRole) userRoleDao.findByExample(studentRole)
		// .get(0), "password11"),
		// new User(800009, "Ектерина", "Касинская", "kate@gmail.com",
		// (UserRole) userRoleDao.findByExample(studentRole)
		// .get(0), "password12"),
		// new User(800010, "Мармалюков", "Константин",
		// "marmal@gmail.com", (UserRole) userRoleDao
		// .findByExample(studentRole).get(0),
		// "password13") };
		User[] users = {
				new User(700001, "Станислав", "Суздаль", "suzdal@gmail.com",
						(UserRole) userRoleDao.findByExample(instructorRole)
								.get(0), "password14"),
				new User(700002, "Алекс", "Перез Чернов", "perez@gmail.com",
						(UserRole) userRoleDao.findByExample(instructorRole)
								.get(0), "password15"),
				new User(700003, "Павел", "Скумс", "skums@gmail.com",
						(UserRole) userRoleDao.findByExample(instructorRole)
								.get(0), "password16"),
				new User(700004, "Светлана", "Панина", "panina@gmail.com",
						(UserRole) userRoleDao.findByExample(instructorRole)
								.get(0), "password17"),
				new User(700005, "Игорь", "Галкин", "galkin@gmail.com",
						(UserRole) userRoleDao.findByExample(instructorRole)
								.get(0), "password18"),
				new User(700006, "Татьяна", "Якименко", "yakimenko@gmail.com",
						(UserRole) userRoleDao.findByExample(instructorRole)
								.get(0), "password18"),
				new User(700007, "Александр", "Шибут", "shibyt@gmail.com",
						(UserRole) userRoleDao.findByExample(instructorRole)
								.get(0), "password19"),
				new User(800011, "Елена", "Либик", "libik@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password20"),
				new User(800012, "Денис", "Воробей", "vorobei@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password21"),
				new User(800013, "Алеся", "Андреюк", "andreuk@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password22"),
				new User(800014, "Тимофей", "Ладик", "ladik@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password23"),
				new User(800015, "Растислав", "Радыно", "radino@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password24"),
				new User(800016, "Ирина", "Черник", "chernik@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password25"),
				new User(800017, "Артем", "Довнар", "dovnar@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password26"),
				new User(800018, "Сергей", "Бондарев", "bondarev@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password27"),
				new User(800019, "Инна", "Лось", "los@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password28"),
				new User(800020, "Виктория", "Ерофеева", "erofeeva@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password29"),
				new User(800021, "Ольга", "Подолякина",
						"podolyakina@gmail.com", (UserRole) userRoleDao
								.findByExample(studentRole).get(0),
						"password30"),
				new User(800022, "Дмитрий", "Юрковский", "urkovskii@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password31"),
				new User(800023, "Любовь", "Жук", "jyk@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password32"),
				new User(800024, "Андрей", "Солодухин", "solodyhin@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password33"),
				new User(800025, "Светлана", "Веселова", "veselova@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password34"),
				new User(800026, "Екатерина", "Виничек", "vinichek@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password35"),
				new User(800027, "Юлия", "Загорская", "zagorskaya@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password36"),
				new User(800028, "Ирина", "Маркевич", "markevich@gmail.com",
						(UserRole) userRoleDao.findByExample(studentRole)
								.get(0), "password37") };
		UserHome userDao = new UserHome();
		for (User u : users) {
			userDao.persist(u);
		}
	}

	/*
	 * Method updates user password encrypting them using MD5 algorithm
	 */
	@SuppressWarnings("unchecked")
	private static void updateUserPasswords() {
		ArrayList<User> users = new ArrayList<User>();
		users.addAll((new UserHome()).findByExample(new User()));
		for (User user : users) {
			if(user.getPassword().length()<11){
				user.setPassword(MD5Encoder.getHash(user.getPassword()));
				(new UserHome()).merge(user);
			}
		}
	}
}